package st.business.tepth.business.maintainplan;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.MaintainPlanBean;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * @author zhangyu
 * @date 2019/6/4
 */
public class MaintainPlanDayDelegate extends MvpFragment<MaintainPlanPresenter>
        implements MaintainPlanContract.View {
    public static MaintainPlanDayDelegate newInstance(String date) {
        Bundle bundle = new Bundle();
        bundle.putString("date", date);
        MaintainPlanDayDelegate delegate = new MaintainPlanDayDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view_tab)
    RecyclerView mRecyclerView;
    @BindView(R2.id.layout_container)
    ConstraintLayout mLayoutContainer;
    private String mDate;
    private int mOldPosition = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDate = bundle.getString("date");
        }
    }

    @Override
    public MaintainPlanPresenter getPresenter() {
        return new MaintainPlanPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_maintain_plan_day;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#ffffff"), 10));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MaintainPlanBean maintainPlanBean = (MaintainPlanBean) baseQuickAdapter.getItem(i);
                if (maintainPlanBean != null && (mOldPosition != i)) {
                    maintainPlanBean.setSelected(true);
                    baseQuickAdapter.notifyItemChanged(i);
                    SupportFragment contentFragment = findChildFragment(FragmentBusInfo.class);
                    if (contentFragment != null) {
                        contentFragment.replaceFragment(FragmentBusInfo
                                .newInstance(maintainPlanBean), true);
                    }
                    MaintainPlanBean old = (MaintainPlanBean) baseQuickAdapter.getItem(mOldPosition);
                    if (old != null) {
                        old.setSelected(false);
                        baseQuickAdapter.notifyItemChanged(mOldPosition);
                    }
                    mOldPosition = i;
                }
            }
        });
        addTopView(R.layout.view_loading, mLayoutContainer);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.getMaintainPlanList(UserManager.getInstance().getToken(), mDate
        );
    }

    @Override
    public void showMaintainPlanList(List<MaintainPlanBean> list) {
        removeTopView(mLayoutContainer);
        if (list == null || list.isEmpty()) {
            addTopView(R.layout.view_bus_empty, mLayoutContainer);
        } else {
            list.get(0).setSelected(true);
            mRecyclerView.setAdapter(new BaseQuickAdapter<MaintainPlanBean, BaseViewHolder>(R.layout
                    .item_maintain_plan, list) {
                @Override
                protected void convert(BaseViewHolder viewHolder, MaintainPlanBean commonBean) {
                    viewHolder.setVisible(R.id.iv_indicator, commonBean.isSelected());
                    viewHolder.setText(R.id.tv_name, commonBean.getName());
                    int bgColor;
                    int textColor;
                    int status = commonBean.getRepairStatus();
                    switch (status) {
                        case 0:
                            bgColor = Color.parseColor("#dce1ea");
                            textColor = Color.parseColor("#5d6775");
                            break;
                        case 1:
                            bgColor = Color.parseColor("#f1b647");
                            textColor = Color.parseColor("#ffffff");
                            break;
                        default:
                            bgColor = Color.parseColor("#1aad6f");
                            textColor = Color.parseColor("#ffffff");
                            break;

                    }
                    viewHolder.setBackgroundColor(R.id.tv_name, bgColor);
                    viewHolder.setTextColor(R.id.tv_name,textColor);
                }
            });
            if (findChildFragment(FragmentBusInfo.class) == null) {
                loadRootFragment(R.id.view_content,
                        FragmentBusInfo
                                .newInstance(list.get(0)),
                        false, false);
            }
        }
    }

    @Override
    public void showError(String msg) {

    }
}
